<?php
declare(strict_types=1);

namespace App\AdminApi\Module\System\Api;

use App\AdminApi\AdminApiBasicsController;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\{Controller, GetMapping, PostMapping};
use Psr\Http\Message\ResponseInterface;
use Zms\AdminHelper\DataBase;
use Zms\Guard\Auth;
use Zms\HyperfResponse\Response;
use function Hyperf\Config\config;

#[
    Auth(guard: self::GUARD, label: ['系统管理', '数据表'], app: self::APP),
    Controller(prefix: "/admin-api/system-db", server: self::SERVER)
]
class DbController extends AdminApiBasicsController
{

    #[
        GetMapping('list'),
        Auth(sign: 'system.db.list', describe: "查看数据表信息", view: '/system/db/index')
    ]
    public function list(): ResponseInterface
    {
        return Response::Api()->success(data: array_values(DataBase::table()));
    }

    #[
        PostMapping('optimize'),
        Auth(label: ['优化表'], sign: 'system.db.optimize')
    ]
    public function optimize(): ResponseInterface
    {
        $connection = $this->request->input('connection');
        if (!in_array($connection, $this->dbname())) {
            return Response::Api()->fail('无效的数据库链接');
        }
        $table = $this->request->input('table');
        if (!(is_string($table) && strlen($table) > 0)) {
            return Response::Api()->fail('没有可优化的数据表');
        }
        $db = DB::connection($connection);
        $tableInfo = $db->selectOne("SHOW TABLE STATUS WHERE `Name`='$table'");
        if (!$tableInfo) {
            return Response::Api()->fail('没有可优化的数据表');
        }
        $db->select("OPTIMIZE TABLE `$table`");
        return Response::Api()->success('数据表优化完成');
    }

    /**
     * 获取所有DB连接
     * @return array
     */
    private function dbname(): array
    {
        return array_keys(config('databases'));
    }

    #[
        PostMapping('repair'),
        Auth(label: ['修复表'], sign: 'system.db.repair')
    ]
    public function repair(): ResponseInterface
    {
        $connection = $this->request->input('connection');
        if (!in_array($connection, $this->dbname())) {
            return Response::Api()->fail('无效的数据库链接');
        }
        $table = $this->request->input('table');
        if (!(is_string($table) && strlen($table) > 0)) {
            return Response::Api()->fail('没有可修复的数据表');
        }
        $db = DB::connection($connection);
        $tableInfo = $db->selectOne("SHOW TABLE STATUS WHERE `Name`='$table'");
        if (!$tableInfo) {
            return Response::Api()->fail('没有可修复的数据表');
        }
        if (strtolower($tableInfo->Engine) == 'innodb') {
            return Response::Api()->fail('储存引擎不支持此操作');
        }
        $db->select("REPAIR TABLE `$table`");
        return Response::Api()->success('数据表修复完成');
    }


    #[
        GetMapping('dictionary'),
        Auth(label: ['导出数据字典'], sign: 'system.db.dictionary')
    ]
    public function dictionary(): ResponseInterface
    {
        return Response::Api()->success(data: DataBase::dictionary());
    }
}